﻿//created by Mai

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataAccessLayer
{
    public class MakeRequisitionDAL
    {
        StationeryInventoryEntities1 ent;

        public MakeRequisitionDAL()
        {
            ent = new StationeryInventoryEntities1();

        }
        public void saveRequisition(DateTime reqDate, int reqBy)
        {

        }

        public int getUserID(string userName)
        {
            User user = ent.Users.FirstOrDefault(x => x.UserName == userName);
            return user.UserID;
        }

        

        public Employee getEmpInfo(String userName)
        {
            int userId = getUserID(userName);
            Employee emp = ent.Employees.FirstOrDefault(x => x.UserID == userId);

            return emp;
        }

        public Department getDeptInfoByEmpID(int empId)
        {
            Employee emp = ent.Employees.First(x => x.EmployeeID ==empId);
            string deptID= emp.DeptID;
            Department d = ent.Departments.FirstOrDefault(x => x.DeptID == deptID);
            return d;
        }

        //to save to Requisition table
        public int saveRequisition(int reqBy)
        {
            Requisition r = new Requisition();
            r.ReqDate = DateTime.Today;
            r.RequestedBy = reqBy;
            r.ApprovalStatus = "Pending";
            ent.Requisitions.Add(r);
            ent.SaveChanges();
            int i = r.ReqID;

            return i;
            
        }

        //to save to requisitionDetailOne table
        public void saveReqDetail(int reqID, String itemCode, int reqQty)
        {
            RequisitionDetailOne rDetailOne = new RequisitionDetailOne();
            rDetailOne.ReqID=reqID;
            rDetailOne.ItemCode = itemCode;
            rDetailOne.RequestQty=reqQty;
            ent.RequisitionDetailOnes.Add(rDetailOne);
            ent.SaveChanges();
        }

        //to get the list of requisitionDetailOne from requisitionID
        public List<RequisitionDetailOne> getReqDetOneByReqID(int reqID)
        {
            var reqDetOneList = (from x in ent.RequisitionDetailOnes where x.ReqID == reqID select x);
            return reqDetOneList.ToList();
        }

        public Stationery getStationeryInfoByItemCode(String itemCode)
        {
            Stationery s = ent.Stationeries.FirstOrDefault(x => x.ItemCode == itemCode);
            return s;
        }
       

    }
}
